/*
 * Copyright 2011 Philippe Blanc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package me.l1k3.fx.client;

import me.l1k3.core.client.Color;
import me.l1k3.core.client.Core;
import me.l1k3.fx.client.channel.ColorChannel;
import me.l1k3.fx.client.impl.FXAbstract;
import me.l1k3.fx.client.inter.FX;

import com.google.gwt.dom.client.Element;

public class FXColor extends FXAbstract {
	private ColorChannel colorChannel;
	
	public FXColor(Color start, Color end) {
		colorChannel = ColorChannel.range(getType(), start, end);
	}
	
	//- - - factory
	
	public final static FXColor color(Element element, Color start, Color end) {
        FXColor highlight = new FXColor(start, end);
        highlight.init(element);
        
        return highlight;
    }
	
	public final static FXColor color(Element element, Color end) {
	    String currentColor = Core.getComputedStyleProperty(element, "color");
	    FXColor highlight = new FXColor(currentColor!=null? Color.parse(currentColor) : new Color(255,255,255), end);
	    highlight.init(element);
	    
	    return highlight;
	}
    
    //- - -    
	
	public void color(Color end) {
	    String currentColor = Core.getComputedStyleProperty(getContext().getElement(), "color");
	    colorChannel.setRange(ColorChannel.COLOR, currentColor!=null? Color.parse(currentColor) : new Color(255,255,255), end);
	}
	
    //- - - 
    
    /**
     * @exclude
     */
    @Override
    public FX init(Element element) {
        super.init(element);
        colorChannel.init(context);
        validateChannel(colorChannel);
        
        return this;
    }
    
    /**
     * @exclude
     */
	@Override
	public FX toggle() {
		colorChannel.toggle(context);
		
		return this;
	}
	
	/**
     * @exclude
     */
	@Override
	protected void onStart() {
        super.onStart();
        colorChannel.start(context);
    }
    
	/**
     * @exclude
     */
    @Override
    public void onUpdate(double progress) {
        context.setProgress(progress);
        colorChannel.update(context);
    }	
	
    /**
     * @exclude
     */
    @Override
    protected void onComplete() {
        colorChannel.complete(context);
        super.onComplete();
    }
 }
